﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace FileDuplicateRemover.Core
{
    public class FileEntity : BaseEntity
    {
        private static HashAlgorithm HashAlg { get; set; }

        static FileEntity()
        {
            //HashAlg = SHA512.Create();
            HashAlg = MD5.Create();
        }        

        private byte[] _Hash;
        public byte[] Hash
        {
            get
            {
                if (_Hash == null)
                {
                    using (Stream s = File.Open(this.FulltName, FileMode.Open))
                    {
                        _Hash = HashAlg.ComputeHash(s);
                        Console.WriteLine("... ComputeHash for " + FulltName);
                    }
                }

                return _Hash;
            }
        }

        public FileEntity(string fullName) : base(fullName)
        {
        }

        public override bool Equals(BaseEntity other)
        {
            if (other != null && other is FileEntity)
                return EqualsCore(other as FileEntity);
            else
                return false;
        }

        private bool EqualsCore(FileEntity fe)
        {
            bool res = this == fe;

            if (!res)
            {
                res = this.Length == fe.Length;

                if (res)
                {
                    //res = this.Hash.SequenceEqual(fe.Hash);
                }
            }

            return res;
        }
    }
}
